package com.threeox.commonlibrary.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.util.TypedValue;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.threeox.commonlibrary.R;
import com.threeox.commonlibrary.ui.dialog.emnu.CommonButtonType;
import com.threeox.commonlibrary.ui.dialog.impl.CommonDialogClickListener;
import com.threeox.commonlibrary.ui.dialog.impl.ICommonDialogBuilder;
import com.threeox.utillibrary.util.res.LayoutUtils;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @ClassName: CommonModelDialogBuilder
 *
 * @Description: Todo()
 *
 * @author 赵屈犇
 *
 * @date 创建时间:2019/6/19 17:37
 * 
 * @version 1.0
 */
public class CommonModelDialogBuilder implements ICommonDialogBuilder {

    private Dialog dialog;
    private Context context;

    private boolean isShowButton = true;
    private View layout, titleView, contentView;
    private TextView titleTextView, contentTextView;
    private LinearLayout titleLayout, contentLayout;
    private String title, content, leftButtonText, rightButtonText, centerButtonText;
    private CommonDialogClickListener leftButtonClickListener, rightButtonClickListener, centerButtonClickLinstener;

    private Map<CommonButtonType, TextView> buttonMap = new HashMap<CommonButtonType, TextView>();

    public CommonModelDialogBuilder(Context context) {
        this.context = context;
        this.dialog = new Dialog(context, R.style.common_dialog_builder);
    }

    @Override
    public ICommonDialogBuilder setTitle(Object title) {
        if (title != null) {
            if (title instanceof Integer) {
                this.title = context.getString((Integer) title);
            } else {
                this.title = String.valueOf(title);
            }
        }
        return this;
    }

    @Override
    public String getTitle() {
        return title;
    }

    @Override
    public ICommonDialogBuilder setTitleView(View view) {
        this.titleView = view;
        return this;
    }

    @Override
    public ICommonDialogBuilder setTitleLayout(int layoutId) {
        this.titleView = LayoutUtils.inflate(context, layoutId);
        return this;
    }

    @Override
    public ICommonDialogBuilder setContent(Object content) {
        if (content != null) {
            if (content instanceof Integer) {
                this.content = context.getString((Integer) content);
            } else {
                this.content = String.valueOf(content);
            }
        }
        return this;
    }

    @Override
    public ICommonDialogBuilder setContentView(View view) {
        this.contentView = view;
        return this;
    }

    @Override
    public ICommonDialogBuilder setContentLayout(int layoutId) {
        this.contentView = LayoutUtils.inflate(context, layoutId);
        return this;
    }

    @Override
    public boolean isShowing() {
        return dialog.isShowing();
    }

    @Override
    public ICommonDialogBuilder show() {
        if (dialog != null) {
            dialog.show();
        }
        return this;
    }

    @Override
    public ICommonDialogBuilder dismiss() {
        if (dialog != null) {
            dialog.dismiss();
        }
        return this;
    }

    @Override
    public ICommonDialogBuilder hideButton(CommonButtonType buttonType) {
        TextView button = buttonMap.get(buttonType);
        if (button != null) {
            button.setVisibility(View.GONE);
        }
        return this;
    }

    @Override
    public ICommonDialogBuilder setShowButtonLayout(boolean isShow) {
        this.isShowButton = isShow;
        return this;
    }

    @Override
    public ICommonDialogBuilder setLeftButton(Object leftText, CommonDialogClickListener listener) {
        if (leftText != null) {
            if (leftText instanceof Integer) {
                this.leftButtonText = context.getString((Integer) leftText);
            } else {
                this.leftButtonText = String.valueOf(leftText);
            }
        }
        this.leftButtonClickListener = listener;
        return this;
    }

    @Override
    public ICommonDialogBuilder setRightButton(Object rightText, CommonDialogClickListener listener) {
        if (rightText != null) {
            if (rightText instanceof Integer) {
                this.rightButtonText = context.getString((Integer) rightText);
            } else {
                this.rightButtonText = String.valueOf(rightText);
            }
        }
        this.rightButtonClickListener = listener;
        return this;
    }

    @Override
    public ICommonDialogBuilder setCenterButton(Object centerText, CommonDialogClickListener listener) {
        if (centerText != null) {
            if (centerText instanceof Integer) {
                this.centerButtonText = context.getString((Integer) centerText);
            } else {
                this.centerButtonText = String.valueOf(centerText);
            }
        }
        this.centerButtonClickLinstener = listener;
        return this;
    }

    @Override
    public Dialog getDialog() {
        return dialog;
    }

    @Override
    public ICommonDialogBuilder setCancelable(boolean isCancel) {
        dialog.setCancelable(isCancel);
        return this;
    }

    @Override
    public ICommonDialogBuilder setCanceledOnTouchOutside(boolean isCancel) {
        dialog.setCanceledOnTouchOutside(isCancel);
        return this;
    }

    @Override
    public <T extends View> T findViewById(int id) {
        return layout.findViewById(id);
    }

    /**
     * 初始化dialog
     *
     * @return
     */
    @Override
    public ICommonDialogBuilder initDialog() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layout = inflater.inflate(R.layout.common_model_dialog, null);
        dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
        int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, context.getResources().getDisplayMetrics());
        // 设置宽度
        lp.width = (display.getWidth() - pageMargin);
        dialog.getWindow().setAttributes(lp);
        /**=========标题==========*/
        titleLayout = findViewById(R.id.id_common_dialog_title_layout);
        titleTextView = findViewById(R.id.id_common_dialog_title_text_view);
        /**=========标题==========*/
        /**=========内容==========*/
        contentLayout = findViewById(R.id.id_common_dialog_content_layout);
        contentTextView = findViewById(R.id.id_common_dialog_content_text_view);
        /**=========内容==========*/
        if (titleView != null) {
            titleLayout.addView(titleView);
            titleTextView.setVisibility(View.GONE);
        } else {
            titleTextView.setText(title);
            titleTextView.setVisibility(View.VISIBLE);
        }
        // 设置中间布局或文字
        // // 设置消息
        if (contentView != null) {
            contentLayout.addView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            contentTextView.setVisibility(View.GONE);
        } else {
            contentTextView.setText(content);
            contentTextView.setVisibility(View.VISIBLE);
        }
        // 设置左边按钮
        setButton(leftButtonText, leftButtonClickListener, R.id.id_common_dialog_left_button_view);
        // 设置右边按钮
        setButton(rightButtonText, rightButtonClickListener, R.id.id_common_dialog_right_button_view);
        // 设置中间按钮
        setButton(centerButtonText, centerButtonClickLinstener, R.id.id_common_dialog_center_button_view);
        // 设置隐藏按钮
        if (!isShowButton) {
            findViewById(R.id.id_common_dialog_button_layout).setVisibility(View.GONE);
        }
        dialog.setContentView(layout);
        return this;
    }

    /**
     * 设置按钮
     *
     * @param btnText
     * @param clickListener
     * @param btnId
     */
    public void setButton(String btnText, final CommonDialogClickListener clickListener, int btnId) {
        // 设置按钮
        if (btnText != null) {
            final TextView dialogRightBtn = findViewById(btnId);
            if (btnId == R.id.id_common_dialog_left_button_view) {
                buttonMap.put(CommonButtonType.LEFT_BUTTON, dialogRightBtn);
            } else if (btnId == R.id.id_common_dialog_right_button_view) {
                buttonMap.put(CommonButtonType.RIGHT_BUTTON, dialogRightBtn);
            } else if (btnId == R.id.id_common_dialog_center_button_view) {
                buttonMap.put(CommonButtonType.CENTER_BUTTON, dialogRightBtn);
            }
            dialogRightBtn.setText(btnText);
            if (clickListener != null) {
                dialogRightBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        clickListener.onClick(CommonModelDialogBuilder.this, dialog, dialogRightBtn.getText().toString());
                    }
                });
            }
        } else {
            // 隐藏按钮
            findViewById(btnId).setVisibility(View.GONE);
        }
    }

}
